package com.wxq.day0420.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.Locale;
import java.util.Properties;

/**
 * @Author wxq
 * @Date 2021/4/20
 **/
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.wxq"})
public class SpringMVCConfig implements WebMvcConfigurer {

    /**
     * 配置视图解析器
     */
    @Bean
    public InternalResourceViewResolver getViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    /**
     * 配置静态资源 不需要DispatcherServlet 转发的请求
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");


    }


    /**
     * 配置消息属性文件errorMessages.properties
     * Bean的名字必须是messageSource,这是Spring规定的
     * 加载国际化资源文件 -->
     * <bean id="messageSource"
     * class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
     * <property name="basename" value="classpath:messages" /> -->
     * <property name="basename" value="/WEB-INF/resource/messages" />
     * </bean>
     *
     * @return
     */
    @Bean("messageSource")
    public ResourceBundleMessageSource getMessageSource() {
        ResourceBundleMessageSource rbms = new ResourceBundleMessageSource();
        //默认去src/main/resources目录下找errorMessages.properties
        rbms.setBasename("messages");
        rbms.setDefaultEncoding("UTF-8");
        return rbms;
    }

    /**
     * 配置SessionLocaleResolver实现的Bean
     */
//
    @Bean("localeResolver")
    public LocaleResolver getLocaleResolver() {
        SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
        Locale loc = new Locale("en", "us");
        //设置默认的语言和国家
        sessionLocaleResolver.setDefaultLocale(loc);
        return sessionLocaleResolver;
    }

    /**
     * 配置LocaleChangeInterceptor拦截器
     * 国际化操作拦截器，如果采用基于Session/Cookie则必须配置
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
        registry.addInterceptor(localeChangeInterceptor).addPathPatterns("/**");
    }


    public SimpleMappingExceptionResolver getExceptionResolver() {
        SimpleMappingExceptionResolver ser = new SimpleMappingExceptionResolver();
        //定义默认的异常处理页面
        ser.setDefaultErrorView("error");
        //定义需要的特殊处理的异常，用类名或者完全路径名作为key，异常页名作为值
        Properties mapping = new Properties();
        mapping.put("com.wxq.day0420.exception.MyException", "my-error");
        mapping.put("java.sql.SQLException", "sql-error");
        ser.setExceptionMappings(mapping);
        return ser;
    }
}